﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class DistrictViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private DistrictModel gDistrictModel;
        

        public DistrictViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gDistrictModel = new DistrictModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            List<WCity> cityList = WCity.GetAll();

            List<WDistrict> list = WDistrict.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                LDistrict item = new LDistrict();
                item.IsChoose = false;
                item.DistrictUID = list[i].DistrictUID;
                item.DistrictName = list[i].DistrictName;
                item.PersonnelUID = list[i].PersonnelUID;
                item.CityUID = list[i].CityUID;
                item.Person.Name = list[i].Person.Name;
                foreach(var city in cityList)
                {
                    if(city.CityUID == item.CityUID)
                    {
                        item.CityName = city.CityName;
                        item.CitySelected = city.CityName;
                        break;
                    }
                }
                DataList.Add(item);
            }
        }

        public ObservableCollection<LDistrict> DataList
        {
            get
            {
                return gDistrictModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        public ObservableCollection<LDistrict> DataListEdit
        {
            get
            {
                return gDistrictModel.DataListEdit;
            }

            set
            {
                DataListEdit = value;
                RaisePropertyChanged("DataListEdit");
            }
        }

        public Visibility DataListEditVisibility
        {
            get
            {
                return gDistrictModel.dataListEditVisibility;
            }
            set
            {
                gDistrictModel.dataListEditVisibility = value;
                RaisePropertyChanged("DataListEditVisibility");
            }
        }

        protected override void DLCloseCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLSaveCommand(object obj)
        {
            List<WDistrict> list = new List<WDistrict>();
            foreach (LDistrict item in DataListEdit)
            {
                if (item.IsChoose)
                {
                    list.Add(new WDistrict(item));
                }
            }
            HttpResponse response = WDistrict.Update(list);

            UpdateList();

            DataListEditVisibility = Visibility.Hidden;
            DLBSaveVisibility = Visibility.Hidden;
            EditMode = false;
        }

        protected override void DLQuitCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLGoCommand(object obj)
        {

        }

        protected override void DLPageUpCommand(object obj)
        {

        }

        protected override void DLPageDownCommand(object obj)
        {

        }

        protected override void DLCheckedCommand(object obj)
        {
            if (DLTIsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void DLAddCommand(object obj)
        {
            DistrictInsert window = new DistrictInsert();
            DistrictInsertViewModel viewModel = new DistrictInsertViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DLEditCommand(object obj)
        {
            if(!EditMode)
            {
                DataListEdit.Clear();

                List<WCity> list = WCity.GetAll();
                foreach (LDistrict item in DataList)
                {
                    if(item.IsChoose)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            item.Cities.Add(list[i].CityName.Trim());
                        }
                        item.CitySelected = item.CityName;

                        DataListEdit.Add(item);
                    }
                }

                if(DataListEdit.Count>0)
                {
                    DataListEditVisibility = Visibility.Visible;
                    DLBSaveVisibility = Visibility.Visible;
                    EditMode = true;
                }
            }
        }

        protected override void DLDeleteCommand(object obj)
        {
            List<WDistrict> list = new List<WDistrict>();
            foreach (LDistrict item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(new WDistrict(item));
                }
            }
            HttpResponse response = WDistrict.Delete(list);

            UpdateList();
        }
    }
}
